﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;


namespace Docentric.Examples.Browser
{
	public class ChartsExample : ExampleBase
	{
		// Constructor
		public ChartsExample()
		{
			Name = "Charts";
			Title = "Charts";
			Description = "This Example shows how data-bound charts can be used in reports. Here we have two different types of charts both bound to the dafult data source which represents collection of 'ProductCategory' objects. In Docentric templates all types of charts provided by MS Word can be used.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Get all product categories.
			List<ProductCategory> productCategories = DataAccess.GetProductCategories();

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(productCategories);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
